package com.link2room.quintesson.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.BatchUtil;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.ironhide.dto.param.inside.HotelBaseInfoInsideParamDto;
import com.link2room.ironhide.dto.result.base.L2RHotelPictureDto;
import com.link2room.quintesson.constant.QuintessonMnsConstant;
import com.link2room.quintesson.constant.QuintessonTaskNameConstant;
import com.link2room.quintesson.dto.param.manager.TaobaoXhotelManagerParamDto;
import com.link2room.quintesson.dto.result.manager.TaobaoHotelPicDto;
import com.link2room.quintesson.dto.result.manager.TaobaoXhotelDto;
import com.link2room.quintesson.entity.TaobaoShotel;
import com.link2room.quintesson.entity.TaobaoXhotel;
import com.link2room.quintesson.exception.TaobaoException;
import com.link2room.quintesson.facade.ITaobaoXhotelFacadeService;
import com.link2room.quintesson.feign.ironhide.HotelBaseInfoFeignService;
import com.link2room.quintesson.service.ITaobaoAPIService;
import com.link2room.quintesson.util.TaobaoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sss on 2017/4/10.
 */
@Component("taobaoXhotelFacadeService")
public class TaobaoXhotelFacadeServiceImpl implements ITaobaoXhotelFacadeService {
    @Resource
    private ITaobaoAPIService taobaoAPIService;
    @Resource
    private ICommonService commonService;
    @Autowired
    private HotelBaseInfoFeignService hotelBaseInfoFeignService;

    /**
     * 飞猪酒店列表
     *
     * @param taobaoXhotelManagerParamDto@return
     * @throws Exception
     */
    @Override
    public QueryResult<TaobaoXhotelDto> listTaobaoXhotel(TaobaoXhotelManagerParamDto taobaoXhotelManagerParamDto)throws Exception{
        TaobaoXhotel queryTXH=new TaobaoXhotel();
        queryTXH.setLhotelGroupCode(taobaoXhotelManagerParamDto.getLhotelGroupCode());
        queryTXH.setLhotelCode(taobaoXhotelManagerParamDto.getLhotelCode());
        QueryResult<TaobaoXhotel> taobaoXhotelResult = commonService.findAllEQ(TaobaoXhotel.class, queryTXH, taobaoXhotelManagerParamDto.getCurrentPage(), taobaoXhotelManagerParamDto.getPageSize());
        QueryResult<TaobaoXhotelDto> taobaoXhotelDtoQueryResult=ClassConverUtil.copyQueryResultToDtoQueryResult(taobaoXhotelResult,TaobaoXhotelDto.class,false);
        if(taobaoXhotelDtoQueryResult.getResultlist()!=null&&taobaoXhotelDtoQueryResult.getResultlist().size()>0){
            for(TaobaoXhotelDto taobaoXhotelDto:taobaoXhotelDtoQueryResult.getResultlist()){
                if(StringUtil.isNotBlank(taobaoXhotelDto.getHotelFacilities())){
                    taobaoXhotelDto.setTaobaoHotelFacilities(JacksonUtil.jsonToBean(taobaoXhotelDto.getHotelFacilities(),Map.class));
                }
                if(StringUtil.isNotBlank(taobaoXhotelDto.getRoomFacilities())){
                    taobaoXhotelDto.setTaobaoRoomFacilities(JacksonUtil.jsonToBean(taobaoXhotelDto.getRoomFacilities(), Map.class));
                }
                if(StringUtil.isNotBlank(taobaoXhotelDto.getService())){
                    taobaoXhotelDto.setTaobaoService(JacksonUtil.jsonToBean(taobaoXhotelDto.getService(), Map.class));
                }
            }
        }
        return taobaoXhotelDtoQueryResult;
    }

    /**
     * 飞猪酒店详情
     * @param baseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public TaobaoXhotelDto findTaobaoXhotel(BaseParamDto baseParamDto)throws Exception{
        TaobaoXhotel taobaoXhotel = commonService.findOneById(TaobaoXhotel.class, baseParamDto.getId());
        TaobaoXhotelDto taobaoXhotelDto = ClassConverUtil.copyPropertiesToDto(taobaoXhotel, TaobaoXhotelDto.class, false);
        if(StringUtil.isNotBlank(taobaoXhotelDto.getHotelFacilities())){
            taobaoXhotelDto.setTaobaoHotelFacilities(JacksonUtil.jsonToBean(taobaoXhotelDto.getHotelFacilities(), Map.class));
        }
        if(StringUtil.isNotBlank(taobaoXhotelDto.getRoomFacilities())){
            taobaoXhotelDto.setTaobaoRoomFacilities(JacksonUtil.jsonToBean(taobaoXhotelDto.getRoomFacilities(), Map.class));
        }
        if(StringUtil.isNotBlank(taobaoXhotelDto.getService())){
            taobaoXhotelDto.setTaobaoService(JacksonUtil.jsonToBean(taobaoXhotelDto.getService(), Map.class));
        }
        return taobaoXhotelDto;
    }

    /**
     * 更新或保存酒店列表
     * @param taobaoXhotelManagerParamDto
     * @throws Exception
     */
    @Override
    public List<TaobaoXhotelDto> saveOrUpdateTaobaoXhotelList(TaobaoXhotelManagerParamDto taobaoXhotelManagerParamDto) throws Exception {
        if(taobaoXhotelManagerParamDto.getTaobaoXhotelDtoList()!=null&& taobaoXhotelManagerParamDto.getTaobaoXhotelDtoList().size()>0){
            List<TaobaoXhotel> taobaoXhotelList=new ArrayList<>();
            for(TaobaoXhotelDto taobaoXhotelDto:taobaoXhotelManagerParamDto.getTaobaoXhotelDtoList()){
                TaobaoXhotel taobaoXhotel=ClassConverUtil.copyProperties(taobaoXhotelDto,TaobaoXhotel.class);
                if(taobaoXhotelDto.getTaobaoHotelFacilities()!=null){
                    taobaoXhotel.setHotelFacilities(JacksonUtil.beanToJson(taobaoXhotelDto.getTaobaoHotelFacilities()));
                }
                if(taobaoXhotelDto.getTaobaoRoomFacilities()!=null){
                    taobaoXhotel.setRoomFacilities(JacksonUtil.beanToJson(taobaoXhotelDto.getTaobaoRoomFacilities()));
                }
                if(taobaoXhotelDto.getTaobaoService()!=null){
                    taobaoXhotel.setService(JacksonUtil.beanToJson(taobaoXhotelDto.getTaobaoService()));
                }
                taobaoXhotelList.add(taobaoXhotel);
            }
            commonService.saveOrUpdateList(taobaoXhotelList);
            return ClassConverUtil.copyPropertiesListToDtoList(taobaoXhotelList,TaobaoXhotelDto.class,false);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXhotelManagerParamDto.toString());
        }
    }

    /**
     * 更新或保存酒店
     * @param taobaoXhotelManagerParamDto
     * @throws Exception
     */
    @Override
    public TaobaoXhotelDto saveOrUpdateTaobaoXhotel(TaobaoXhotelManagerParamDto taobaoXhotelManagerParamDto) throws Exception {
        if(taobaoXhotelManagerParamDto.getTaobaoXhotelDto()!=null){
            TaobaoXhotelDto taobaoXhotelDto = taobaoXhotelManagerParamDto.getTaobaoXhotelDto();
            TaobaoXhotel taobaoXhotel=ClassConverUtil.copyProperties(taobaoXhotelDto,TaobaoXhotel.class);
            if(taobaoXhotelDto.getTaobaoHotelFacilities()!=null){
                taobaoXhotel.setHotelFacilities(JacksonUtil.beanToJson(taobaoXhotelDto.getTaobaoHotelFacilities()));
            }
            if(taobaoXhotelDto.getTaobaoRoomFacilities()!=null){
                taobaoXhotel.setRoomFacilities(JacksonUtil.beanToJson(taobaoXhotelDto.getTaobaoRoomFacilities()));
            }
            if(taobaoXhotelDto.getTaobaoService()!=null){
                taobaoXhotel.setService(JacksonUtil.beanToJson(taobaoXhotelDto.getTaobaoService()));
            }
            commonService.saveOrUpdate(taobaoXhotel);
            return ClassConverUtil.copyPropertiesToDto(taobaoXhotel,TaobaoXhotelDto.class,false);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXhotelManagerParamDto.toString());
        }
    }

    /**
     * 上传飞猪酒店列表
     * @param taobaoXhotelManagerParamDto
     * @throws Exception
     */
    @Override
    public void upTaobaoXhotelList(TaobaoXhotelManagerParamDto taobaoXhotelManagerParamDto) throws Exception {
        if(taobaoXhotelManagerParamDto.getIds()!=null&& taobaoXhotelManagerParamDto.getIds().size()>0){
            List<TaobaoXhotelManagerParamDto> taobaoXhotelManagerParamDtos =new ArrayList<>();
            for(Long id: taobaoXhotelManagerParamDto.getIds()){
                TaobaoXhotelManagerParamDto tbManagerParamDto=new TaobaoXhotelManagerParamDto();
                tbManagerParamDto.setId(id);
                taobaoXhotelManagerParamDtos.add(tbManagerParamDto);
            }
            BatchUtil.beginBatchTask(QuintessonMnsConstant.QUEUE_UP_TAOBAO_XHOTEL, QuintessonTaskNameConstant.UP_TAOBAO_XHOTEL, taobaoXhotelManagerParamDtos);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXhotelManagerParamDto.toString());
        }
    }

    /**
     * 上传飞猪酒店
     * @param taobaoXhotelManagerParamDto
     * @throws Exception
     */
    @Override
    public void upTaobaoXhotel(TaobaoXhotelManagerParamDto taobaoXhotelManagerParamDto) throws Exception {
        Long id=taobaoXhotelManagerParamDto.getId();
        TaobaoXhotel taobaoXhotel=commonService.findOneById(TaobaoXhotel.class,id);
        if(taobaoXhotel!=null){
            TaobaoUtil.setTaobaoClient(taobaoXhotelManagerParamDto.getLhotelGroupCode());
            TaobaoXhotelDto taobaoXhotelDto = ClassConverUtil.copyPropertiesToDto(taobaoXhotel, TaobaoXhotelDto.class, false);
            HotelBaseInfoInsideParamDto hotelBaseInfoInsideParamDto =new HotelBaseInfoInsideParamDto();
            hotelBaseInfoInsideParamDto.setLhotelCode(taobaoXhotelDto.getLhotelCode());
            List<String> lhotelCodes=new ArrayList<>();
            lhotelCodes.add(taobaoXhotelDto.getLhotelCode());
            hotelBaseInfoInsideParamDto.setLhotelCodes(lhotelCodes);
            List<TaobaoHotelPicDto> taobaoHotelPicDtos=new ArrayList<>();
            QueryResult<L2RHotelPictureDto> hotelBigPicsResult = hotelBaseInfoFeignService.hotelBigPics(hotelBaseInfoInsideParamDto.paramToMap()).getQueryResult();
            if(hotelBigPicsResult.getResultlist()!=null&&hotelBigPicsResult.getResultlist().size()>0){
                L2RHotelPictureDto hotelBigPic = hotelBigPicsResult.getResultlist().get(0);
                taobaoHotelPicDtos.add(new TaobaoHotelPicDto(hotelBigPic.getSrc(),DefaultValue.TRUE,hotelBigPic.getTypeDes(),hotelBigPic.getAttributeDes()));
            }
            QueryResult<L2RHotelPictureDto> hotelPicsResult = hotelBaseInfoFeignService.hotelPics(hotelBaseInfoInsideParamDto.paramToMap()).getQueryResult();
            if(hotelPicsResult.getResultlist()!=null&&hotelPicsResult.getResultlist().size()>0) {
                for(L2RHotelPictureDto hotelPic:hotelPicsResult.getResultlist()){
                    taobaoHotelPicDtos.add(new TaobaoHotelPicDto(hotelPic.getSrc(),DefaultValue.FALSE,hotelPic.getTypeDes(),hotelPic.getAttributeDes()));
                }
            }
            taobaoXhotelDto.setPics(JacksonUtil.beanToJson(taobaoHotelPicDtos));
            taobaoXhotelDto = taobaoAPIService.syncTaobaoXhotel(taobaoXhotelDto);

            commonService.updateById(ClassConverUtil.copyProperties(taobaoXhotelDto,TaobaoXhotel.class));
            if(taobaoXhotelDto.getTaobaoShotelDto()!=null){
                TaobaoShotel queryTSH=new TaobaoShotel();
                queryTSH.setLhotelGroupCode(taobaoXhotelDto.getLhotelGroupCode());
                queryTSH.setLhotelCode(taobaoXhotelDto.getLhotelCode());
                TaobaoShotel taobaoShotel = commonService.findOneEQ(TaobaoShotel.class, queryTSH);
                if(taobaoShotel==null){
                    taobaoShotel=new TaobaoShotel();
                    ClassConverUtil.copyProperties(taobaoXhotelDto.getTaobaoShotelDto(),taobaoShotel);
                }else {
                    taobaoShotel.setShotel(taobaoXhotelDto.getTaobaoShotelDto().getShotel());
                }
                commonService.saveOrUpdate(taobaoShotel);
            }
        }
        BatchUtil.doBatchTaskDone(taobaoXhotelManagerParamDto.getTaskMainId(),
                taobaoXhotelManagerParamDto.getTaskId(),
                taobaoXhotelManagerParamDto.getIsBatch());

    }
}
